package com.bingbing.back.controller;

import com.bingbing.back.bean.User;
import com.bingbing.back.common.bean.AjaxResult;
import com.bingbing.back.common.utils.AllPool;
import com.bingbing.back.mapper.ConsultInfoMapper;
import com.bingbing.back.mapper.UserMapper;
import com.bingbing.back.po.ConsultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin
@RestController
public class UserController {

    @Autowired(required=false)
    private UserMapper userMapper;
    @Autowired(required=false)
    private ConsultInfoMapper consultInfoMapper;


    // 注册用户
    @GetMapping("/register")
    public AjaxResult<?> register(@RequestParam String name) {
        String s = userMapper.selectByName(name);
        if (s != null){
            return AjaxResult.failure("用户已存在");
        }
        User user = new User();
        user.setStuName(name);
        userMapper.insert(user);
        return AjaxResult.success();
    }

    // 登录
    @GetMapping("/login")
    public AjaxResult<?> login(@RequestParam String name){
        System.out.println(name);
        User user = userMapper.selectUserByName(name);
        return AjaxResult.success(user);
    }

    @GetMapping("/consultlogin")
    public AjaxResult<?> consultlogin(@RequestParam String name,@RequestParam String consultPwd){
        ConsultInfo ConsultInfo = consultInfoMapper.selectUserByNameAndPassword(name,consultPwd);
        if (ConsultInfo == null || ConsultInfo.getConsultId() == null){
            return AjaxResult.failure("该用户不存在");
        }
        return AjaxResult.success(ConsultInfo);
    }


}
